package cn.sx.yys.utils;

import java.security.SecureRandom;

/**
 * 随机数工具类
 *
 * @author 猿帅
 * @email 1603194187@qq.com
 * @date 2020/12/9 14:32
 */
public class RandomUtils {

    private static char[] chars = {
            'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l',
            'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x',
            'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9'
    };
    private static char[] charsNum = {
            '0', '1', '2', '3', '4', '5', '6', '7', '8', '9'
    };

    /**
     * 随机数生成器
     */
    private static SecureRandom random = new SecureRandom();

    /**
     * 生成指定长度的字符串
     *
     * @param length
     * @return
     */
    public static String nonce(int length) {
        int size = chars.length;
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < length; i++) {
            sb.append(chars[random.nextInt(size)]);
        }
        return sb.toString();
    }

    /**
     * 生成指定长度的纯数字字符串
     *
     * @param length
     * @return
     */
    public static String nonceNum(int length) {
        int size = charsNum.length;
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < length; i++) {
            sb.append(charsNum[random.nextInt(size)]);
        }
        return sb.toString();
    }
}
